package com.tensquare.rest;

import com.netflix.discovery.converters.Auto;
import com.tensquare.entity.Admin;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.entity.User;
import com.tensquare.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户类
 */
@RestController
@RequestMapping("user")
public class UserRest {

    @Autowired
    UserService userService;
    @Autowired
    BCryptPasswordEncoder bcry;


    /**
     *
     * @param name
     * @param password
     * @return
     */
    @RequestMapping(value = "/login/{name}/{password}", method = RequestMethod.GET)
    public Result login(@PathVariable("name") String name, @PathVariable("password") String password){

        Admin admin = userService.login(name, password);

        if ((admin != null && bcry.matches(password, admin.getPassword()))){

            return new Result(true, StatusCode.OK, "登录成功", admin);
        }else {
            return new Result(false, StatusCode.LOGINGERROR, "用户名或密码错误");
        }

    }

    /**
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result getById(@PathVariable("id") String id){
        User user = new User();
        user.setId((Math.random() * 100000) + "");
        user.setNickname("王二小");
        System.err.println(user);
        return new Result(true, StatusCode.OK, "查询成功", user);
    }

}
